package com.yb.guigu.pick.auth.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.yb.guigu.pick.core.bean.GuiguResult;
import com.yb.guigu.pick.core.bean.WechatConfig;
import com.yb.guigu.pick.core.bean.WechatLoginResult;
import com.yb.guigu.pick.auth.service.IMemberUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/6/25 16:26
 * FileName: MemberUserServiceImpl
 * Description:
 */

@Service
@Slf4j
public class MemberUserServiceImpl implements IMemberUserService {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    WechatConfig wechatConfig;
    @Override
    public GuiguResult login(String code) {

        String loginUrlFormat = wechatConfig.getLoginUrlFormat();

        String loginUrl = String.format(loginUrlFormat, wechatConfig.getAppId(), wechatConfig.getAppSecret(), code);

        ResponseEntity<String> forEntity = restTemplate.getForEntity(loginUrl, String.class);
        String entityBody = forEntity.getBody();


        WechatLoginResult wechatLoginResult = JSONObject.parseObject(entityBody, WechatLoginResult.class);
        return GuiguResult.success(wechatLoginResult);
    }
}
